const state = {
  all: [],
  result: ''
}

const getters = {
  getAllLength: (state) => {
    return state.all.length
  },
  getResult: (state, getters) => {
    var length = getters.getAllLength
    var obj = {
      allLength: length,
      result: 'success',
      code: function () { return 0 },
      tostr: function () { return 'result: ' + this.result + 'code: ' + this.code() + 'allLength: ' + this.allLength }
    }
    return obj
  },
  getFinalResult: (state, getters) => (id) => {
    alert('id: ' + id)
    return state.all.find(item => item === id)
  }
}

const actions = {
  asycAddData ({ commit, state }, temp) {
    // alert('temp:' + temp)
    // setInterval(() => { }, 1000)
    setTimeout(() => {
      var datas = ['du', 'ning', 'wei']
      datas = datas.concat(temp)
      // alert('datas: ' + datas)
      commit('setData', datas)
    }, 4000)
  }
}

const mutations = {
  setData (state, datas) {
    state.all = datas
  },
  findStr (state, str) {
    // alert('all: ' + state.all)
    state.result = state.all.find(item => item === str)
  }
}

export default {
  // namespaced: true, 开启：指定module区间 this.$store.dispatch('test/asycAddData',temp)  不开启：this.$store.dispatch('asycAddData',temp)
  state,
  getters,
  actions,
  mutations
}
